/* @(#)GSOrderDeliverStatusTraceQueryAction.java
 *
 * Project: BUS
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   RoadZheng     2006-3-17    建立
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room,
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun,
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 *
 */

package com.hitrust.gs.action;

import java.util.ArrayList;
import java.util.HashMap;

import com.hitrust.gs.bean.GSOperatorBean;
import com.hitrust.gs.business.GSManufactureProcessor;
import com.hitrust.gs.business.GSOrderDeptInfoProcessor;
import com.hitrust.gs.business.GSOrderProcessor;
import com.hitrust.gs.dao.GSOrderDAO;
import com.hitrust.gs.form.GSOrderStatusTraceQueryForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.service.GSBaseManager;
import com.hitrust.gs.util.date.DateUtil;
import com.hitrust.gs.util.page.ResultProcess;

public class GSOrderDeliverStatusTraceQueryAction extends GSBaseAction {

	public String execute(GSBaseForm aForm) throws Exception {
		GSOrderStatusTraceQueryForm tForm = (GSOrderStatusTraceQueryForm) aForm;
		try {
			
			tForm.getRequest().setAttribute("orderid", tForm.getOrderid());
			tForm.getRequest().setAttribute("providerName", tForm.getProviderName());
			tForm.getRequest().setAttribute("concentricityId", tForm.getConcentricityId());
			tForm.getRequest().setAttribute("orderCreateDateStr", tForm.getOrderCreateDateStr());
			tForm.getRequest().setAttribute("orderCreateDateEnd", tForm.getOrderCreateDateEnd());
			tForm.getRequest().setAttribute("manufInfo", tForm.getManufInfo());
            
            String[] tDateStr = new String[2];
            tDateStr[0] = tForm.getOrderCreateDateStr();
            tDateStr[1] = tForm.getOrderCreateDateEnd();
            tDateStr = DateUtil.formateDate(tDateStr);
            tForm.setOrderCreateDateStr(tDateStr[0]);
            tForm.setOrderCreateDateEnd(tDateStr[1]);
            
			GSOperatorBean tOperatorBean = (GSOperatorBean) aForm.getSession()
					.getAttribute(GSBaseConstants.LOGIN);
			// 取得当前操作员可操作企业及下级企业的订货地址
			GSOrderDeptInfoProcessor tOrderDeptInfoProc = new GSOrderDeptInfoProcessor();
			HashMap tMap = tOrderDeptInfoProc
					.getOrderDeptInfoOfBusOper(tOperatorBean.getGs_oper_org_id());
			aForm.getRequest().setAttribute("orderdept", tMap);
 
			GSOrderProcessor tProc = new GSOrderProcessor();
			ArrayList tOrg = (ArrayList)tMap.get("organization");
            //取得分页参数
            String[] tStr = this.getPageParams(tForm,tOrg);
            //执行分页查询
			ArrayList tList = tProc.getOrderDeliverStatusList(tForm,tOrg,Integer.parseInt(tStr[0]),Integer.parseInt(tStr[1]));
			ResultProcess tRsProc = new ResultProcess();
			tRsProc.setResult(tList);
			tForm.getRequest().setAttribute("order", tRsProc);
			
            //取得产品分类
            GSManufactureProcessor tManufProc = new GSManufactureProcessor();
            tManufProc.getManufactureOfLevel(aForm);
            
            //存储分页参数
            tForm.getRequest().setAttribute("pageSize",tStr[1]);
            tForm.getRequest().setAttribute("totalRecords",tStr[2]);
            //存储总金额
            if(tForm.getChoice()!=null)
                tForm.getRequest().setAttribute("totalAmt",String.valueOf(tForm.getTotalAmt()));
            else{
                tForm.getRequest().setAttribute("totalAmt",tStr[3]);
            }

		} catch (Exception e) {
			tForm.getRequest().setAttribute("action", "A101101.do");
			throw e;
		}
		return GSBaseConstants.FORWARD_SUCCESS;
	}
    private String[] getPageParams(GSOrderStatusTraceQueryForm aForm,ArrayList aOrg) throws Exception{
        String tStr[] = new String[4];
        int totalRecords;
        int pageNo;
        int pageSize;
        String tPage = GSBaseConstants.tPage;
        //根据页面传入的查询参数查询订单信息
        if(aForm.getChoice() == null || aForm.getChoice().equals("")){
            GSOrderDAO tDAO = new GSOrderDAO();
            String[] temp = new String[2];
            temp = tDAO.getOrderDeliverStatuslstCount(aForm,aOrg);
            totalRecords = Integer.parseInt(temp[0]);
            // 取得系统默认的页大小
            pageSize = Integer.parseInt(tPage);
            // PageNo=1
            pageNo = 1;
            int n = (totalRecords % pageSize == 0 )? totalRecords / pageSize:totalRecords / pageSize + 1;
            if (pageNo>n) {
                pageNo=n;
            }
            tStr[0] = "" + pageNo;
            tStr[1] = "" + pageSize;
            tStr[2] = "" + totalRecords;
            tStr[3] = temp[1];
        }else{
            tStr = GSBaseManager.getPageNoAndPageSize(aForm);
        }
        return tStr;
    }
}
